package com.bupt.vueblog.service.impl;

import com.bupt.vueblog.dao.BriefArticleDAO;
import com.bupt.vueblog.dao.CommentDAO;
import com.bupt.vueblog.dao.RelationDAO;
import com.bupt.vueblog.domain.dto.ChangeNumDTO;
import com.bupt.vueblog.domain.dto.DetailInfoDTO;
import com.bupt.vueblog.domain.entity.Comment;
import com.bupt.vueblog.domain.entity.Relation;
import com.bupt.vueblog.domain.vo.BriefCommentVO;
import com.bupt.vueblog.domain.vo.CommentVO;
import com.bupt.vueblog.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 谭继好
 * @Description
 * @date 2024-04-09 13:50
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDAO commentDAO;

    @Autowired
    private RelationDAO relationDAO;

    @Autowired
    private BriefArticleDAO briefArticleDAO;

    @Override
    // 查具体博客信息时查出来下面的评论，不会单独调用，在博客的service层调用
    public List<CommentVO> getByArticle(Integer userId, Integer articleId) {
        List<CommentVO> res = commentDAO.getByArticleId(userId, articleId);
        return res;
    }

    @Override
    // 查询用户发表所有博客下的评论
    public List<BriefCommentVO> getByUserArticles(Integer userId) {
        List<Integer> list = briefArticleDAO.selectIdByUser(userId);
        if (list != null && list.size() != 0) {
            List<BriefCommentVO> res = commentDAO.getByArticleIds(userId, list);
            return res;
        }
        return null;
    }

    @Override
    // 查询对于用户评论的评论
    public List<BriefCommentVO> getByUserComments(Integer userId) {
        List<Integer> commentIds = commentDAO.getByUserId(userId);
        if (commentIds != null && commentIds.size() != 0) {
            List<BriefCommentVO> res = commentDAO.getByParentIds(commentIds);
            return res;
        }
        return null;
    }

    @Override
    public List<BriefCommentVO> getByUser(int userId) {
        List<BriefCommentVO> res = commentDAO.getByUser(userId);
        return res;
    }

    @Override
    @Transactional
    // 添加评论的同时修改评论数。这里的newNum传入增量，而非具体的值
    public void insert(Comment comment) {
        commentDAO.insert(comment);
        ChangeNumDTO temp = new ChangeNumDTO(comment.getArticleId(), 1, 2);
        briefArticleDAO.changeNum(temp);
    }

    @Override
    public void changeInfo(DetailInfoDTO data) {
        commentDAO.changeInfo(data);
    }

    @Override
    @Transactional
    public void addNum(Relation data) {
        commentDAO.changeNum(data);
        relationDAO.insert(data);
    }

    @Override
    @Transactional
    public void subNum(Relation data) {
        commentDAO.changeNum(data);
        relationDAO.delete(data);
    }

    @Override
    @Transactional
    // 删除评论及关联的点赞关系，依赖于该评论的评论变为一级评论
    // 同时修改评论数
    public void delete(Integer id, Integer articleId) {
        commentDAO.delete(id);
        relationDAO.deleteComment(id);
        commentDAO.changeParent(id);
        ChangeNumDTO temp = new ChangeNumDTO(articleId, -1, 2);
        briefArticleDAO.changeNum(temp);
    }

    @Override
    @Transactional
    // 删除用户所有评论及对应点赞关系，同时将依赖于评论的评论改为一级评论
    // 仅仅是为了删除用户时的事务控制，并不会单独调用
    public void deleteUser(Integer userId) {
        List<Integer> ids = commentDAO.getByUserId(userId);
        // ids不为空，有评论再调用删除，否则SQL语句报错
        if (ids != null && ids.size() != 0) {
            commentDAO.changeParents(ids);
        }
        commentDAO.deleteUser(userId);
        relationDAO.deleteCommentUser(userId);
    }

}
